package com.qiantu.phone.ui.adapter;

import android.content.Context;
import android.view.ViewGroup;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import androidx.annotation.NonNull;

import com.qiantu.phone.BuildConfig;
import com.qiantu.phone.R;
import com.qiantu.phone.app.AppAdapter;

import java.util.HashMap;

public final class DeviceFilterItemAdapter extends AppAdapter<Integer> {

    private final int TYPE_ALL = 1;
    public static final int ITEM_ALL = 11;
    public static final int ITEM_IS_DISABLE = 12;
    public static final int ITEM_IS_FAVOURITE = 13;
    public static final int ITEM_IS_HOME_SHOW = 14;
    private HashMap<Integer, Boolean> selected = new HashMap<>();

    public DeviceFilterItemAdapter(@NonNull Context context) {
        super(context);
    }

    @NonNull
    @Override
    public AppAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        switch (viewType){
            case TYPE_ALL:
                return new AllViewHolder();
            default:
                return new ViewHolder();
        }
    }

    private final class ViewHolder extends AppAdapter<?>.ViewHolder {

        private final RadioGroup radioGroup;
        private final RadioButton btn1;
        private final RadioButton btn2;

        private ViewHolder() {
            super(R.layout.item_device_filter);
            radioGroup = findViewById(R.id.radio_group);
            btn1 = findViewById(R.id.btn1);
            btn2 = findViewById(R.id.btn2);
        }

        @Override
        public void onBindView(int position) {
            Integer value = getData().get(position);
            if(value != null){
                switch (value){
                    case ITEM_IS_DISABLE:
                        btn1.setText(R.string.start_used);
                        btn2.setText(R.string.stop_used);
                        if(selected.get(ITEM_ALL) == null || !selected.get(ITEM_ALL)){
                            if(selected.get(value) == null){
                                radioGroup.clearCheck();
                            }else{
                                if(selected.get(value)){
                                    btn2.setChecked(true);
                                }else{
                                    btn1.setChecked(true);
                                }
                            }
                        }else{
                            radioGroup.clearCheck();
                        }
                        break;
                    case ITEM_IS_FAVOURITE:
                        btn1.setText(R.string.commonly_used);
                        btn2.setText(R.string.not_commonly_used);
                        if(selected.get(ITEM_ALL) == null || !selected.get(ITEM_ALL)){
                            if(selected.get(value) == null){
                                radioGroup.clearCheck();
                            }else{
                                if(selected.get(value)){
                                    btn1.setChecked(true);
                                }else{
                                    btn2.setChecked(true);
                                }
                            }
                        }else{
                            radioGroup.clearCheck();
                        }
                        break;
                    case ITEM_IS_HOME_SHOW:
                        btn1.setText(R.string.home_page_show);
                        btn2.setText(R.string.home_page_hide);
                        if(selected.get(ITEM_ALL) == null || !selected.get(ITEM_ALL)){
                            if(selected.get(value) == null){
                                radioGroup.clearCheck();
                            }else{
                                if(selected.get(value)){
                                    btn1.setChecked(true);
                                }else{
                                    btn2.setChecked(true);
                                }
                            }
                        }else{
                            radioGroup.clearCheck();
                        }
                        break;
                }
            }
        }
    }

    private final class AllViewHolder extends AppAdapter<?>.ViewHolder {

        private AllViewHolder() {
            super(R.layout.item_filter_all);
        }

        @Override
        public void onBindView(int position) {
            Integer type = getItem(position);
            if(selected.get(type) != null){
                itemView.setSelected(selected.get(type));
            }else{
                itemView.setSelected(false);
            }
        }
    }

    @Override
    public int getItemViewType(int position) {
        if(getData().get(position) == ITEM_ALL){
            return TYPE_ALL;
        }else{
            return super.getItemViewType(position);
        }
    }

    public HashMap<Integer, Boolean> getSelected() {
        return selected;
    }

    public void setSelected(Integer type, boolean selected) {
        if(type == ITEM_ALL){//选中全部，需要清空其他选中的
            if(selected){
                this.selected.clear();
            }
        }else{//选中其他的，取消全部
            this.selected.put(ITEM_ALL, false);
        }
        this.selected.put(type, selected);
        notifyDataSetChanged();
    }

    @Override
    public long getItemId(int position) {
        return getData().get(position).hashCode();
    }
}